package com.neca.auction.pages;

import java.util.Date;
import java.util.List;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.annotations.Inject;
import org.hibernate.Query;
import org.hibernate.Session;

import com.neca.auction.util.HibernateUtility;

import com.neca.auction.dbb.DBBroker;
import com.neca.auction.model.CurrentUser;
import com.neca.auction.model.User;


/**
 * Start page of application auction.
 */
public class Start
{
	private String userName;
	private String password;
	@Inject 
	private Messages messages;
	@Component
	private Form loginForm;
	@ApplicationState
	private CurrentUser currentUser;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	void onSubmitFromLoginForm(){
		System.out.println("Form was submitted!!!");
		
		
	}
	
	void onValidate(){
		/*if(userName.equals("neca") && password.equals("neca")){
			currentUser=new CurrentUser(userName);
		}*/
		User user=new User();
		user.setUserName(userName);
		user.setPassword(password);
		DBBroker dbbroker=DBBroker.getDbbroker();
		if(dbbroker.logIn(user)){
			currentUser=new CurrentUser(userName);
			System.out.println(currentUser.getUserName());
		}
		else{
			loginForm.recordError(messages.get("authentication-failed"));
		}
		
	}

	Object onSuccess(){
		System.out.println("Successsss!!!!");
	return MainNavigation.class;
}
	void onActivate(){
		currentUser=null;
		
	}
	
	
}